/*************************************************************************
 Copyright (C) Unpublished Unis Software, Inc. All rights reserved.
 Unis Software, Inc., Confidential and Proprietary.

 This software is subject to copyright protection
 under the laws of the Public of China and other countries.

 Unless otherwise explicitly stated, this software is provided
 by Unis "AS IS".
 *************************************************************************/
package com.unis.common.ui.jqGrid;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.unis.model.BaseModel;

/**
 * @author CSJ (email:raulcsj@163.com)
 * @since 2012-12-4
 */
public final class GridUIMapper {
    private GridUIMapper() {}

    public static final Map<String, Object> render(BaseModel baseModel, List<BaseModel> baseModels) {
        Map<String, Object> renderMap = new HashMap<String, Object>();

        int totalCount = baseModel.getTotalCount();
        int pageSize = baseModel.getLimit() / baseModel.getPage();
        int total = (int) Math.ceil(Double.valueOf(totalCount) / pageSize);
        int page = baseModel.getPage();

        renderMap.put("page", page > total ? total : page);// 第几页
        renderMap.put("total", total);// 总页数
        renderMap.put("records", totalCount);// 总记录数
        renderMap.put("rows", baseModels);// 具体查询的数据

        return renderMap;
    }

    public static final Map<String, Object> process(Map<?, ?> paramMap) {
        // 分页参数
        int page = Integer.parseInt(paramMap.get("page").toString());// 请求页码
        page = page > 1 ? page : 1;
        int rows = Integer.parseInt(paramMap.get("rows").toString());// 记录条数
        Object sidx = paramMap.get("sidx");// 按哪一列排序
        Object sord = paramMap.get("sord");// 排序方式(asc/desc)
        Map<String, Object> renderMap = new HashMap<String, Object>();
        renderMap.put("page", page);
        renderMap.put("start", (page - 1) * rows);
        renderMap.put("limit", page * rows);
        renderMap.put("sidx", sidx);
        renderMap.put("sord", sord);
        return renderMap;
    }
}
